package com.shure.modules.docs.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shure.modules.docs.entity.Docs;
import com.shure.modules.docs.query.DocsQuery;
import com.shure.modules.docs.service.DocsService;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/docs")
public class DocsController {

    private final DocsService docsService;

    public DocsController(DocsService docsService) {
        this.docsService = docsService;
    }

    // 新增文档
    @PostMapping
    public ResponseEntity<Long> create(@RequestBody Docs docs) {
        Docs d = docsService.isave(docs);
        return ResponseEntity.ok(d.getId());
    }

    // 查询文档列表
    @GetMapping("list")
    public ResponseEntity<Page<Docs>> list(@Validated DocsQuery query) {
        return ResponseEntity.ok(docsService.ipage(query));
    }

    // 查询文档
    @GetMapping("/{id}")
    public ResponseEntity<Docs> getById(@PathVariable("id") Long id) {
        Docs doc = docsService.getById(id);
        return ResponseEntity.ok(doc);
    }

    // 更新文档
    @PutMapping("/{id}")
    public ResponseEntity<Boolean> update(@PathVariable("id") Long id, @RequestBody Docs docs) {
        docs.setId(id);
        return ResponseEntity.ok(docsService.updateById(docs));
    }

    // 删除文档
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> remove(@PathVariable("id") Long id) {
        Docs docs = docsService.getById(id);
        docs.setStatus(0);
        docsService.updateById(docs);
        return ResponseEntity.noContent().build();
    }

    // 物理删除
    @DeleteMapping("delete/{id}")
    public ResponseEntity<Void> delete(@PathVariable("id") Long id) {
        docsService.removeById(id);
        return ResponseEntity.noContent().build();
    }


}
